﻿#pragma once

#ifndef BRIDGEANALYSIS_ORG_FILE_H
#define BRIDGEANALYSIS_ORG_FILE_H

int64_t createOrgFile_(const char *szOrgName, bool innerCall = true);
int64_t openOrgFile_(const char *szFileName, bool innerCall = true);
bool saveOrgFile_(int64_t nHandle, const char *szFileName, bool innerCall = true);
bool closeOrgFile_(int64_t nHandle, bool innerCall = true);
bool getOrgHead_(int64_t nHandle, TOrgHead *pBuffer, bool innerCall = true);
bool setOrgHead_(int64_t nHandle, TOrgHead *pBuffer, bool innerCall = true);
bool getOrgInstCond_(int64_t nHandle, int32_t nSigChannel, TInstCond *pBuffer, bool innerCall = true);
bool setOrgInstCond_(int64_t nHandle, int32_t nSigChannel, TInstCond *pBuffer, bool innerCall = true);
bool getOrgRawData_(int64_t nHandle, int32_t nSigChannel, TRawItem *pBuffer, int32_t nBufferCount, int32_t *pDataCount, bool innerCall = true);
bool setOrgRawData_(int64_t nHandle, int32_t nSigChannel, const TRawItem *pBuffer, int32_t nBufferCount, bool innerCall = true);
bool getOrgAnalResult_(int64_t nHandle, int32_t nSigChannel, TAnalResult *pBuffer, int32_t nBufferCount, int32_t *pDataCount, bool innerCall = true);
bool setOrgAnalResult_(int64_t nHandle, int32_t nSigChannel, const TAnalResult *pBuffer, int32_t nBufferCount, bool innerCall = true);
bool getOrgSynResult_(int64_t nHandle, int32_t nSigChannel, TSynResult *pBuffer, int32_t nBufferCount, int32_t *pDataCount, bool innerCall = true);
bool setOrgSynResult_(int64_t nHandle, int32_t nSigChannel, const TSynResult *pBuffer, int32_t nBufferCount, bool innerCall = true);
bool getOrgStudyData_(int64_t nHandle, int32_t nSigChannel, TRawItem *pBuffer, int32_t nBufferCount, int32_t *pDataCount, bool innerCall = true);
bool setOrgStudyData_(int64_t nHandle, int32_t nSigChannel, const TRawItem *pBuffer, int32_t nBufferCount, bool innerCall = true);
bool getOrgBlankData_(int64_t nHandle, int32_t nSigChannel, TRawItem *pBuffer, int32_t nBufferCount, int32_t *pDataCount, bool innerCall = true);
bool setOrgBlankData_(int64_t nHandle, int32_t nSigChannel, const TRawItem *pBuffer, int32_t nBufferCount, bool innerCall = true);
bool getOrgBlankResult_(int64_t nHandle, int32_t nSigChannel, TAnalResult *pBuffer, int32_t nBufferCount, int32_t *pDataCount, bool innerCall = true);
bool setOrgBlankResult_(int64_t nHandle, int32_t nSigChannel, const TAnalResult *pBuffer, int32_t nBufferCount, bool innerCall = true);
bool analysis_(int64_t nHandle, int64_t nMethodHandle, bool innerCall = true);

BRIDGE6DLL_API int32_t getOrgMapSize();

// 3.4.1、创建谱图
BRIDGE6DLL_API int64_t createOrgFile(const char *szOrgName);

// 3.4.2、打开谱图
BRIDGE6DLL_API int64_t openOrgFile(const char *szFileName);

// 3.4.3、存储谱图
BRIDGE6DLL_API bool saveOrgFile(int64_t nHandle, const char *szFileName);

// 3.4.4、关闭谱图
BRIDGE6DLL_API bool closeOrgFile(int64_t nHandle);

// 3.4.5、获取谱图基础数据
BRIDGE6DLL_API bool getOrgHead(int64_t nHandle, TOrgHead *pBuffer);
BRIDGE6DLL_API TOrgHead *getOrgHeadDirect(int64_t nHandle);

// 3.4.6、设置谱图基础数据
BRIDGE6DLL_API bool setOrgHead(int64_t nHandle, TOrgHead *pBuffer);

// 3.4.7、获取谱图仪器条件
BRIDGE6DLL_API bool getOrgInstCond(int64_t nHandle, int32_t nSigChannel, TInstCond *pBuffer);

// 3.4.8、设置谱图仪器条件
BRIDGE6DLL_API bool setOrgInstCond(int64_t nHandle, int32_t nSigChannel, TInstCond *pBuffer);

// 3.4.9、获取谱图原始数据
BRIDGE6DLL_API bool getOrgRawData(int64_t nHandle, int32_t nSigChannel, TRawItem *pBuffer, int32_t nBufferCount, int32_t *pDataCount);

// 3.4.10、设置谱图原始数据
BRIDGE6DLL_API bool setOrgRawData(int64_t nHandle, int32_t nSigChannel, const TRawItem *pBuffer, int32_t nBufferCount);

// 3.4.11、获取谱图分析结果
BRIDGE6DLL_API bool getOrgAnalResult(int64_t nHandle, int32_t nSigChannel, TAnalResult *pBuffer, int32_t nBufferCount, int32_t *pDataCount);

// 3.4.12、设置谱图分析结果
BRIDGE6DLL_API bool setOrgAnalResult(int64_t nHandle, int32_t nSigChannel, const TAnalResult *pBuffer, int32_t nBufferCount);

// 3.4.13、获取谱图综合结果
BRIDGE6DLL_API bool getOrgSynResult(int64_t nHandle, int32_t nSigChannel, TSynResult *pBuffer, int32_t nBufferCount, int32_t *pDataCount);

// 3.4.14、设置谱图综合结果
BRIDGE6DLL_API bool setOrgSynResult(int64_t nHandle, int32_t nSigChannel, const TSynResult *pBuffer, int32_t nBufferCount);

// 3.4.15、获取谱图学习基线数据
BRIDGE6DLL_API bool getOrgStudyData(int64_t nHandle, int32_t nSigChannel, TRawItem *pBuffer, int32_t nBufferCount, int32_t *pDataCount);

// 3.4.16、设置谱图学习基线数据
BRIDGE6DLL_API bool setOrgStudyData(int64_t nHandle, int32_t nSigChannel, const TRawItem *pBuffer, int32_t nBufferCount);

// 3.4.17、获取谱图空白数据
BRIDGE6DLL_API bool getOrgBlankData(int64_t nHandle, int32_t nSigChannel, TRawItem *pBuffer, int32_t nBufferCount, int32_t *pDataCount);

// 3.4.18、设置谱图空白数据
BRIDGE6DLL_API bool setOrgBlankData(int64_t nHandle, int32_t nSigChannel, const TRawItem *pBuffer, int32_t nBufferCount);

// 3.4.19、获取谱图空白结果
BRIDGE6DLL_API bool getOrgBlankResult(int64_t nHandle, int32_t nSigChannel, TAnalResult *pBuffer, int32_t nBufferCount, int32_t *pDataCount);

// 3.4.20、设置谱图空白结果
BRIDGE6DLL_API bool setOrgBlankResult(int64_t nHandle, int32_t nSigChannel, const TAnalResult *pBuffer, int32_t nBufferCount);

// 3.4.21、重新积分
BRIDGE6DLL_API bool analysis(int64_t nHandle, int64_t nMethodHandle);

#ifdef _USRDLL
#define CHECK_ORG_HANDLE(nHandle)                                                                 \
	{                                                                                             \
		if (OrgFileMap.count(nHandle) <= 0)                                                       \
		{                                                                                         \
			SET_ERROR_(ERR_CODE_ORG_HANDLE_NOT_EXIST, ERR_MESSAGE_ORG_HANDLE_NOT_EXIST, nHandle); \
			return false;                                                                         \
		}                                                                                         \
	}

#define CHECK_ORG_CHANNEL(channelNo)                                                                              \
	{                                                                                                             \
		if (!isValidChannel(OrgFile.get()->Detector, channelNo))                                                  \
		{                                                                                                         \
			std::vector<int32_t> validChannels;                                                                   \
			for (int32_t chn = 0; chn < C4; chn++)                                                                \
			{                                                                                                     \
				if (isValidChannel(OrgFile.get()->Detector, chn))                                                 \
				{                                                                                                 \
					validChannels.push_back(chn);                                                                 \
				}                                                                                                 \
			}                                                                                                     \
			SET_ERROR_(ERR_CODE_IPARAM_VALUE_INVALID, ERR_MESSAGE_IPARAM_VALUE_INVALID_1, "channelNo", channelNo, \
					   vector2String(validChannels).c_str());                                                     \
			return false;                                                                                         \
		}                                                                                                         \
	}

extern std::map<int64_t, std::shared_ptr<TOrgFile>> OrgFileMap;
#endif

#endif